package com.googlecode.wmlynar.javadoctc.analyzers.code;

import com.googlecode.wmlynar.javadoctc.domain.JavaDocClass;
import com.googlecode.wmlynar.javadoctc.domain.JavaDocClassPool;
import com.googlecode.wmlynar.javadoctc.domain.JavaDocMethod;
import com.thoughtworks.qdox.JavaDocBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;

public class DocClassesCollector {

	public JavaDocClassPool collect(String packageName, JavaDocBuilder builder) {

		JavaDocClassPool pool = new JavaDocClassPool();

		JavaClass[] classes = builder.getClasses();
		for(JavaClass clas : classes) {
			if(!clas.getFullyQualifiedName().startsWith(packageName)) {
				continue;
			}

			//System.out.println(clas.getFullyQualifiedName());

			JavaDocClass c = new JavaDocClass(clas.getFullyQualifiedName());

			JavaMethod[] methods = clas.getMethods();
			for(JavaMethod method : methods) {
				if(!method.isPublic()) {
					continue;
				}
				if(method.getComment()==null) {
					// TODO: add no comment warning
					continue;
				}
				//System.out.println(method.getName());
				// remove html tags
				String comment = method.getComment().replaceAll("\\<[^>]*>","");
				// remove white space
				comment = comment.replaceAll("\\s"," ");
				//System.out.println(comment);

				JavaDocMethod m = new JavaDocMethod();
				//m.setName(QdoxUtils.getMethodSignature(method));
				m.setName(method.getDeclarationSignature(true));
				m.setJavadoc(comment);
				c.addMethod(m);
			}

			pool.addClass(c);
		}

		return pool;
	}

}
